package  
{
	import flash.events.Event;
	import flash.events.MouseEvent;
	import IInterface.IComponent;
	/**
	 * ...
	 * @author jun
	 */
	public class MMInteractive extends MMComponent implements IComponent 
	{
		private var _onClick:Function;
		private var _onDoubleClick:Function;
		private var _onRollOver:Function;
		private var _onRollOut:Function;
		private var _onMouseOver:Function;
		private var _onMouseOut:Function;
		private var _onMouseMove:Function;
		private var _onMouseDown:Function;
		private var _onMouseUp:Function;
		private var _onMouseWheel:Function;
		private var stopPropagation:Boolean;
		
		public function MMInteractive() 
		{
			super();
			stopPropagation = true;
			addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
			
		}
		
		private function onRemoveFromStage(e:Event):void 
		{
			removeEventListener(Event.REMOVED_FROM_STAGE, onRemoveFromStage);
			addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
			
			if (hasEventListener(MouseEvent.CLICK) && onClick!=null)
			{
				removeEventListener(MouseEvent.CLICK, onClickHandler );
			}
			if (hasEventListener(MouseEvent.DOUBLE_CLICK) && onDoubleClick !=null)
			{
				removeEventListener(MouseEvent.DOUBLE_CLICK, onDoubleClickHandler );
			}
			if (hasEventListener(MouseEvent.ROLL_OVER) && onRollOver !=null)
			{
				removeEventListener(MouseEvent.ROLL_OVER, onRollOverHandler );
			}
			if (hasEventListener(MouseEvent.ROLL_OUT) && onRollOut !=null)
			{
				removeEventListener(MouseEvent.ROLL_OUT, onRollOutHandler);
			}
			if (hasEventListener(MouseEvent.MOUSE_OVER) && onMouseOver!=null)
			{
				removeEventListener(MouseEvent.MOUSE_OVER, onMouseOverHandler );
			}
			if (hasEventListener(MouseEvent.MOUSE_OUT) && onMouseOut!=null)
			{
				removeEventListener(MouseEvent.MOUSE_OUT, onMouseOutHandler);
			}
			if (hasEventListener(MouseEvent.MOUSE_MOVE) && onMouseMove !=null)
			{
				removeEventListener(MouseEvent.MOUSE_MOVE, onMouseMoveHandler);
			}
			if (hasEventListener(MouseEvent.MOUSE_DOWN) && onMouseDown !=null)
			{
				removeEventListener(MouseEvent.MOUSE_DOWN, onMouseDownHandler);
			}
			if (hasEventListener(MouseEvent.MOUSE_UP) && onMouseUp !=null)
			{
				removeEventListener(MouseEvent.MOUSE_UP, onMouseUpHandler);
			}
			if (hasEventListener(MouseEvent.MOUSE_WHEEL) && onMouseWheel !=null)
			{
				removeEventListener(MouseEvent.MOUSE_WHEEL, onMouseWheelHandler);
			}
			
		}
		
		private function onAddedToStage(e:Event):void 
		{
			removeEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
			addEventListener(Event.REMOVED_FROM_STAGE, onRemoveFromStage);
			
			if (!hasEventListener(MouseEvent.CLICK) && onClick !=null)
			{
				addEventListener(MouseEvent.CLICK, onClickHandler );
			}
			if (!hasEventListener(MouseEvent.DOUBLE_CLICK) && onDoubleClick !=null)
			{
				addEventListener(MouseEvent.DOUBLE_CLICK, onDoubleClickHandler);
			}
			if (!hasEventListener(MouseEvent.ROLL_OVER) && onRollOver !=null)
			{
				addEventListener(MouseEvent.ROLL_OVER, onRollOverHandler);
			}
			if (!hasEventListener(MouseEvent.ROLL_OUT) && onRollOut !=null)
			{
				addEventListener(MouseEvent.ROLL_OUT, onRollOutHandler);
			}
			if (!hasEventListener(MouseEvent.MOUSE_OVER) && onMouseOver !=null)
			{
				addEventListener(MouseEvent.MOUSE_OVER, onMouseOverHandler);
			}
			if (!hasEventListener(MouseEvent.MOUSE_OUT) && onMouseOut !=null)
			{
				addEventListener(MouseEvent.MOUSE_OUT, onMouseOutHandler);
			}
			if (!hasEventListener(MouseEvent.MOUSE_MOVE) && onMouseMove !=null)
			{
				addEventListener(MouseEvent.MOUSE_MOVE, onMouseMoveHandler);
			}
			if (!hasEventListener(MouseEvent.MOUSE_DOWN) && onMouseDown !=null)
			{
				addEventListener(MouseEvent.MOUSE_DOWN, onMouseDownHandler);
			}
			if (!hasEventListener(MouseEvent.MOUSE_UP) && onMouseUp !=null)
			{
				addEventListener(MouseEvent.MOUSE_UP, onMouseUpHandler);
			}
			if (!hasEventListener(MouseEvent.MOUSE_WHEEL) && onMouseWheel !=null)
			{
				addEventListener(MouseEvent.MOUSE_WHEEL, onMouseWheelHandler);
			}
		}
		
		private function onMouseWheelHandler(e:MouseEvent):void 
		{
			
			onMouseWheel && onMouseWheel( e);
			if (stopPropagation) e.stopPropagation();
		}
		
		private function onMouseUpHandler(e:MouseEvent):void 
		{
			
			onMouseUp && onMouseUp(e);
			if (stopPropagation) e.stopPropagation();
		}
		
		private function onMouseDownHandler(e:MouseEvent):void 
		{
			
			onMouseDown && onMouseDown( e);
			if (stopPropagation) e.stopPropagation();
		}
		
		private function onMouseMoveHandler(e:MouseEvent):void 
		{
			
			onMouseMove && onMouseMove( e);
			if (stopPropagation) e.stopPropagation();
		}
		
		private function onMouseOutHandler(e:MouseEvent):void 
		{
			onMouseOut && onMouseOut( e);
			if (stopPropagation) e.stopPropagation();
		}
		
		private function onMouseOverHandler(e:MouseEvent):void 
		{
			
			onMouseOver && onMouseOver( e);
			if (stopPropagation) e.stopPropagation();
		}
		
		private function onRollOutHandler(e:MouseEvent):void 
		{
			onRollOut && onRollOut(e);
			if (stopPropagation) e.stopPropagation();
		}
		
		private function onRollOverHandler(e:MouseEvent):void 
		{
			onRollOver && onRollOver(e);
			if (stopPropagation) e.stopPropagation();
		}
		
		private function onDoubleClickHandler(e:MouseEvent):void 
		{
			if (stopPropagation) e.stopPropagation();
			onDoubleClickHandler && onDoubleClickHandler( e);
		}
		
		private function onClickHandler(e:MouseEvent):void 
		{
			if (stopPropagation) e.stopPropagation();
			onClick && onClick(e);
		}
		
		
		
		/* INTERFACE IComponent */
		
		override public function onShow():void 
		{
			super.onShow();
		}
		
		override public function onInit():void 
		{
			super.onInit();
		}
		
		override public function onHide():void 
		{
			super.onHide();
			
		}
		
		override public function updateData():void 
		{
			super.updateData();
		}
		
		override public function regResource():void 
		{
			super.regResource();
		}
		
		public function get onClick():Function { return _onClick; }
		
		public function set onClick(value:Function):void 
		{
			if (hasEventListener(MouseEvent.CLICK))
			{
				removeEventListener(MouseEvent.CLICK, onClickHandler);
			}
			_onClick = value;
			if (value !=null)
			{
				addEventListener(MouseEvent.CLICK, onClickHandler);
			}
		}
		
		public function get onDoubleClick():Function { return _onDoubleClick; }
		
		public function set onDoubleClick(value:Function):void 
		{
			if (hasEventListener(MouseEvent.DOUBLE_CLICK))
			{
				removeEventListener(MouseEvent.DOUBLE_CLICK, onDoubleClickHandler);
			}
			_onDoubleClick = value;
			if (value !=null)
			{
				addEventListener(MouseEvent.DOUBLE_CLICK, onDoubleClickHandler);
			}
		}
		
		public function get onRollOver():Function { return _onRollOver; }
		
		public function set onRollOver(value:Function):void 
		{
			
			if (hasEventListener(MouseEvent.ROLL_OVER))
			{
				removeEventListener(MouseEvent.ROLL_OVER, onRollOverHandler);
			}
			_onRollOver = value;
			if (value !=null)
			{
				addEventListener(MouseEvent.ROLL_OVER, onRollOverHandler);
			}
			
		}
		
		public function get onRollOut():Function { return _onRollOut; }
		
		public function set onRollOut(value:Function):void 
		{
			
			if (hasEventListener(MouseEvent.ROLL_OUT))
			{
				removeEventListener(MouseEvent.ROLL_OUT, onRollOutHandler);
			}
			_onRollOut = value;
			if (value !=null)
			{
				addEventListener(MouseEvent.ROLL_OUT, onRollOutHandler);
			}
			
		}
		
		public function get onMouseOver():Function { return _onMouseOver; }
		
		public function set onMouseOver(value:Function):void 
		{
			if (hasEventListener(MouseEvent.MOUSE_OVER))
			{
				removeEventListener(MouseEvent.MOUSE_OVER, onMouseOverHandler);
			}
			_onMouseOver = value;
			if (value !=null)
			{
				addEventListener(MouseEvent.MOUSE_OVER, onMouseOverHandler);
			}
			
		}
		
		public function get onMouseOut():Function { return _onMouseOut; }
		
		public function set onMouseOut(value:Function):void 
		{	
			if (hasEventListener(MouseEvent.MOUSE_OUT))
			{
				removeEventListener(MouseEvent.MOUSE_OUT, onMouseOutHandler);
			}
			_onMouseOut = value;
			if (value !=null)
			{
				addEventListener(MouseEvent.MOUSE_OUT, onMouseOutHandler);
			}
			
		}
		
		public function get onMouseMove():Function { return _onMouseMove; }
		
		public function set onMouseMove(value:Function):void 
		{
			if (hasEventListener(MouseEvent.MOUSE_MOVE))
			{
				removeEventListener(MouseEvent.MOUSE_MOVE, onMouseMoveHandler);
			}
			_onMouseMove = value;
			if (value !=null)
			{
				addEventListener(MouseEvent.MOUSE_MOVE, onMouseMoveHandler);
			}
		}
		
		public function get onMouseDown():Function { return _onMouseDown; }
		
		public function set onMouseDown(value:Function):void 
		{
			if (hasEventListener(MouseEvent.MOUSE_DOWN))
			{
				removeEventListener(MouseEvent.MOUSE_DOWN, onMouseDownHandler);
			}
			_onMouseDown = value;
			if (value !=null)
			{
				addEventListener(MouseEvent.MOUSE_DOWN, onMouseDownHandler);
			}
			
		}
		
		public function get onMouseUp():Function { return _onMouseUp; }
		
		public function set onMouseUp(value:Function):void 
		{
			if (hasEventListener(MouseEvent.MOUSE_UP))
			{
				removeEventListener(MouseEvent.MOUSE_UP, onMouseUpHandler);
			}
			_onMouseUp = value;
			if (value !=null)
			{
				addEventListener(MouseEvent.MOUSE_UP, onMouseUpHandler);
			}
		}
		
		public function get onMouseWheel():Function { return _onMouseWheel; }
		
		public function set onMouseWheel(value:Function):void 
		{
			if (hasEventListener(MouseEvent.MOUSE_WHEEL))
			{
				removeEventListener(MouseEvent.MOUSE_WHEEL, onMouseWheelHandler);
			}
			_onMouseWheel = value;
			if (value !=null)
			{
				addEventListener(MouseEvent.MOUSE_WHEEL, onMouseWheelHandler);
			}
			
		}
		
		override public function gc():void
		{
			if(typeof (_onClick) !="undefined") _onClick = null;
			if(typeof (_onDoubleClick) !="undefined") _onDoubleClick = null;
			if(typeof (_onMouseDown) !="undefined") _onMouseDown = null;
			if(typeof (_onMouseUp) !="undefined") _onMouseUp = null;
			if(typeof (_onMouseMove) !="undefined") _onMouseMove = null;
			if(typeof (_onMouseOut) !="undefined") _onMouseOut = null;
			if(typeof (_onMouseOver) !="undefined") _onMouseOver = null;
			if(typeof (_onMouseWheel) !="undefined") _onMouseWheel = null;
			if(typeof (_onRollOut) !="undefined") _onRollOut = null;
			
			super.gc();
		}
		
	}

}